package edu.cmu.ibill.controller;

import java.util.*;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;

import edu.cmu.ibill.util.StringProcessor;
import edu.cmu.ibill.databeans.User;
import edu.cmu.ibill.model.CategoryDictDAO;
import edu.cmu.ibill.model.Model;
import edu.cmu.ibill.model.UserDAO;

/**
 * 
 * @author Yunkai Zhang
 * 15-637
 */


public class RegisterAction extends Action {

	private UserDAO userDAO;
	private CategoryDictDAO catDAO;
	public RegisterAction(Model model) {
		userDAO = model.getUserDAO();
		catDAO = model.getCategoryDictDAO();
	}

	@Override
	public String getName() {
		return "register.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("registererrors", errors);

		// Get the email and password that user enters at the login page.
		String email = request.getParameter("registeremail");
		String password = request.getParameter("password");

		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String passwordConfirm = request.getParameter("confirmpassword");

		email = StringProcessor.process(email);
		firstName = StringProcessor.process(firstName);
		lastName = StringProcessor.process(lastName);

		HttpSession session = request.getSession();

		request.setAttribute("registeremail", email);
		request.setAttribute("firstname", firstName);
		request.setAttribute("lastname", lastName);
		request.setAttribute("password", password);

		User user = new User(email);
		user.setPassword(password);
		
		if (email == null || email.trim().length() == 0) {
			errors.add("email is empty");
		}else{
			if(!Pattern.matches(".+@.+", email)){
				errors.add("invalid format for email address");
			}
		}
		if (firstName == null || firstName.trim().length() == 0) {
			errors.add("first name is empty");
		}
		if (lastName == null || lastName.trim().length() == 0) {
			errors.add("last name is empty");
		}
		if (password == null || password.length() == 0) {
			errors.add("password is empty");
		}
		if (passwordConfirm == null) {
			errors.add("passwords do not match");
		} else if (!passwordConfirm.equals(password)) {
			errors.add("passwords do not match");
		}

		if (errors.size() != 0){
			request.setAttribute("registerinprocess", "");
			return "login.jsp";
		}
		else {
			if (firstName.length() > 30)
				firstName = firstName.substring(0, 30);
			if (lastName.length() > 30)
				lastName = lastName.substring(0, 30);
			user.setFirstname(firstName);
			user.setLastname(lastName);
			try {
				userDAO.create(user);
			} catch (DAOException e) {
				errors.add(e.getMessage());
				request.setAttribute("registerinprocess", "");
				return "login.jsp";
			}
			User dbuser = userDAO.Lookup(user.getEmailAddress());
			session.setAttribute("user", dbuser);
			request.setAttribute("categorydict", catDAO.getAllCategoryNames());
			return "manage.jsp";
		}
	}

}
